package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.PracticeCadetExperience;
import com.ruoyi.system.domain.PracticeCadetExperienceCount;
import com.ruoyi.system.service.IPracticeCadetExperienceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 实践心得Controller
 *
 * @author dp
 * @date 2023-11-27
 */
@Api(tags = {"实践心得"})
@RestController
@RequestMapping("/practice/experience")
public class PracticeCadetExperienceController extends BaseController {
    @Autowired
    private IPracticeCadetExperienceService practiceCadetExperienceService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询实践心得列表
     */
    @ApiOperation(value = "实践心得列表", notes = "查询实践心得列表详情", tags = {"实践心得管理"})
    //@PreAuthorize("@ss.hasPermi('practice:experience:list')")
    @GetMapping("/list")
    public TableDataInfo list(PracticeCadetExperience practiceCadetExperience) {
        startPage();
        List<PracticeCadetExperience> list = practiceCadetExperienceService.selectPracticeCadetExperienceList(practiceCadetExperience);
        return getDataTable(list);
    }

    /**
     * 查询实践心得合计列表
     */
    @ApiOperation(value = "实践心得合计列表", notes = "查询实践心得列表详情", tags = {"实践心得管理"})
    //@PreAuthorize("@ss.hasPermi('practice:experience:list')")
    @GetMapping("/countList")
    public TableDataInfo countList(@RequestParam("submitBegin") String submitBegin, @RequestParam("submitEnd") String submitEnd,
                                   @RequestParam("baseId") String baseId,
                                   @RequestParam("experienceStatus") String experienceStatus) {
        startPage();
        List<PracticeCadetExperienceCount> list = practiceCadetExperienceService.selectPracticeCadetExperienceCount(submitBegin, submitEnd, baseId, experienceStatus);
        return getDataTable(list);
    }

    @ApiOperation(value = "实践心得列表", notes = "查询实践心得列表详情", tags = {"实践心得管理"})
    @GetMapping("/getExperienceList")
    public TableDataInfo getExperienceList(@RequestParam("submitBegin") String submitBegin, @RequestParam("submitEnd") String submitEnd,
                                           @RequestParam("baseId") String baseId, @RequestParam("cadetId") String cadetId,
                                           @RequestParam("experienceStatus") String experienceStatus) {
        startPage();
        List<PracticeCadetExperience> list = practiceCadetExperienceService.getExperienceList(submitBegin, submitEnd, baseId, cadetId, experienceStatus);
        return getDataTable(list);
    }

    /**
     * 导出实践心得列表
     */
    @ApiOperation(value = "实践心得列表导出", notes = "导出实践心得列表", tags = {"实践心得管理"})
    //@PreAuthorize("@ss.hasPermi('practice:experience:export')")
    @Log(title = "实践心得", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PracticeCadetExperience practiceCadetExperience) {
        List<PracticeCadetExperience> list = practiceCadetExperienceService.selectPracticeCadetExperienceList(practiceCadetExperience);
        ExcelUtil<PracticeCadetExperience> util = new ExcelUtil<PracticeCadetExperience>(PracticeCadetExperience.class);
        util.exportExcel(response, list, "实践心得数据");
    }

    /**
     * 获取实践心得详细信息
     */
    @ApiOperation(value = "实践心得详细信息", notes = "获取实践心得详细信息详情", tags = {"实践心得管理"})
    //@PreAuthorize("@ss.hasPermi('practice:experience:query')")
    @GetMapping(value = "/{experienceId}")
    public AjaxResult getInfo(@PathVariable("experienceId") Long experienceId) {
        return success(practiceCadetExperienceService.selectPracticeCadetExperienceByExperienceId(experienceId));
    }

    /**
     * 新增实践心得
     */
    @ApiOperation(value = "新增实践心得", notes = "新增实践心得详情", tags = {"实践心得管理"})
    //@PreAuthorize("@ss.hasPermi('practice:experience:add')")
    @Log(title = "实践心得", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody PracticeCadetExperience practiceCadetExperience) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        practiceCadetExperienceService.completeData(practiceCadetExperience, userId, 1);
        return toAjax(practiceCadetExperienceService.insertPracticeCadetExperience(practiceCadetExperience));
    }

    /**
     * 修改实践心得
     */
    @ApiOperation(value = "修改实践心得", notes = "修改实践心得详情", tags = {"实践心得管理"})
    //@PreAuthorize("@ss.hasPermi('practice:experience:edit')")
    @Log(title = "实践心得", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody PracticeCadetExperience practiceCadetExperience) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        practiceCadetExperienceService.completeData(practiceCadetExperience, userId, 2);
        return toAjax(practiceCadetExperienceService.updatePracticeCadetExperience(practiceCadetExperience));
    }

    /**
     * 删除实践心得
     */
    @ApiOperation(value = "删除实践心得", notes = "删除实践心得详情", tags = {"实践心得管理"})
    //@PreAuthorize("@ss.hasPermi('practice:experience:remove')")
    @Log(title = "实践心得", businessType = BusinessType.DELETE)
    @DeleteMapping("/{experienceIds}")
    public AjaxResult remove(@PathVariable Long[] experienceIds) {
        return toAjax(practiceCadetExperienceService.deletePracticeCadetExperienceByExperienceIds(experienceIds));
    }
}
